%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
clear;
clc;
%close all;
%cd D:\000_UIH_Work\520_FUDAN_PHD\450_US_System\201_GEN_Script\matlab_script;

format short g;             %% Set Data Disp Format

%% Global Parameter
GBL_MatlabSimModEN  = 0;      
ByteInvEn           = 0;    %% Down DMA Big-End Inverse Enable
 
%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
%% System Parameter, determine by system physics
SYS_SoundSpeed              = 1540;                     %% m/s 
SYS_PulseTick               = 5   * 1e-9;               %% 5ns Clock Tick
SYS_PulseFreq               = 200 * 1e6;                %% 200MHz Clock
SYS_PCIeTick                = 8   * 1e-9;               %% PCIe Clock 8ns
SYS_AFEClkTick              = 25  * 1e-9;               %% ADC Sample Period
SYS_UltrasoundCH            = 64;                       %% 200MHz Clock

SYS_TGC_Segment             = 8;                        %% Number of Hardware TGC Segment
SYS_M_Trig_CycleDeadTime    = 0.1;                      %% Constant Parameter, 10% is Deadtime
SYS_F_Trig_CycleDeadTime    = 0.1;                      %% Constant Parameter, 10% is Deadtime
SYS_PulserConstantLatency   = 450 * 1e-9;               %% Pulser Constant Latency (Pulser DS.Page43), 0.45us (26 + 64 Pulser Clock = 90 * 5ns = 450ns)

SYS_DataProcessDelay        = 1000;                     %% 1000 * 5ns = 5us ??, Fixed
SYS_SNL_DelayCnt            = 30;                       %% 30 Tick of Pulser Clock (Tick)    

%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
%% Generate Scan Period Parameter
%% User  Parameter, Input by Key-button

%% Set 1: Trigger From Internal FPGA1
%% Set 2: Trigger From Internal FPGA2
%% Set 3: Trigger From Exteranl FPGA3 -> FFPGA1/2, ACQ 128 Channel
%% Set 4: Trigger From Internal FPGA3 -> FFPGA1/2, ACQ 128 Channel
USR_EXTTriggerSlect         = 1;     

USR_UltrasoundBaseFreq      = 2.5 * 1e6;                %% Tranducer Centor Frequency
USR_ImageDepth              = 80  * 1e-3;               %% 80mm
USR_TranducerThick          = 1e-3;                     %% Tranducer Thick, 1mm

USR_ExtTrigNumber           = 10;                   	%% 100-External Trigger, 1-Minitus, ACQ = 4 * 48 = 192 Second, 3Minuts
USR_ExtTriggerPeriod        = 100 * 1e-3;               %% 100ms

USR_FrameNumber             = 1;                        %% Number of Average-Average or Package In One Ext/Internal Trigger, Two Files 
USR_LineNumber              = 6;                        %% Constant

USR_HighVoltageSelect       = 'A';      
 
%% Explaintation
%% 'E' = EOP, '0' = ZERO, '+' VPP, '-' VNN
%% [23:21], [20:18], [17:15], [14:12], [11:09], [08:06], [05:03], [02:00]
%%    +        +        +        +   _    -        -        -        -   
%%    +        +        -        -   _    +        +        -        -  
%%    +        -        +        -   _    +        -        +        -  
%%   EOP
USR_PulsePattern_Code       = '+-; _E';  
USR_PulsePattern_EOP        = 'E';                      %% Laser Transimite Mode
USR_PatternRptCycleNum      = 0;                        %% Each Same NormalPulse will be Repeat 3 Times, could be set by User                                           
USR_EOP_RptCycleNum         = 0;        
USR_EOP_PulseWidth          = 1;
USR_LaserTX_PulseWidth      = 1;                        %% 26 + 64 Tick of Pulser Clock (Tick) 

%%-------------------------------------------------------------------------------------------------------------------------------                                                        
%%-------------------------------------------------------------------------------------------------------------------------------
%% Simulation mode, Reduce Data Size, for Matlab Display

if (GBL_MatlabSimModEN == 1)
    USR_ExtTrigNumber       = 3;                        %% External Trigger = 3 Times
    USR_ExtTriggerPeriod    = 0.3 * 1e-3;               %% 200us, 2*6*8us = 96us
    USR_FrameNumber         = 2;                        %% Number of Average-Average or Package In One Ext/Internal Trigger, Two Files 
    USR_LineNumber          = 6;                        %% Constant
    USR_ImageDepth          = 4   * 1e-3;               %% 4mm
    USR_TranducerThick      = 0.1 * 1e-3;               %% Tranducer Thick, 0.1mm
    
    USR_PulsePattern_Code   = '++++_----; ++--_++--; +-+-_+-+-; _E'; 
    USR_PulsePattern_EOP    = 'E'; 
    USR_PatternRptCycleNum  = 1;                        %% Each NormalPulse will be Repeat 3 Times, could be set by User
    USR_EOP_RptCycleNum     = 0;        
    USR_EOP_PulseWidth      = 1;
    USR_LaserTX_PulseWidth  = 1;                       %% 26 + 64 Tick of Pulser Clock (Tick) 
end

%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
DEV_FramePeriod             = (USR_ExtTriggerPeriod * (1- SYS_M_Trig_CycleDeadTime)) / USR_FrameNumber;
DEV_LinePeriod              = (DEV_FramePeriod * (1- SYS_F_Trig_CycleDeadTime)) / USR_LineNumber;

DEV_ACQ_SampleDuration      = USR_ImageDepth * 2  / SYS_SoundSpeed;
DEV_ACQ_SampleNumber        = DEV_ACQ_SampleDuration / SYS_AFEClkTick;

DEV_Pulse2AFE_GlithTime     = USR_TranducerThick * 2 / SYS_SoundSpeed; 
DEV_Pulse2AFE_GlithCNT      = DEV_Pulse2AFE_GlithTime / SYS_PulseTick;   


%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
%% Generate PWT Waveform (EOP & Normal Wave)
%% Waveform Database
%% To Generate File PWT Memory File 
[PWT_PaternSize GenPWT_Memory_NormalPulse] = fun_GenPWT_MemoryData(USR_PulsePattern_Code, USR_HighVoltageSelect);
[NOP_PaternSize GenPWT_Memory_EOP_Only   ] = fun_GenPWT_MemoryData(USR_PulsePattern_EOP , USR_HighVoltageSelect);

%% Generate PWT Tab File
%% Table Struxture 
%% Hearder
%% 0x00: SYS_PulsePattern_EOP_Only
%% 0x01: Normal PWT Memory
%% Only When Non Simulation Mode, Generate Related Data File 
if (GBL_MatlabSimModEN ~= 1) 

    FileID = fopen('../output_cmd_script/DMA_PWT_Memory.txt', 'w+');
    FileHearder_Length          = length(GenPWT_Memory_EOP_Only) + length(GenPWT_Memory_NormalPulse);
    FileHearder_PWT_DataType    = hex2dec('20');                        %% PWT memory is 0x20
    FileHearder_PWT             = FileHearder_Length * 2^40 + FileHearder_PWT_DataType * 2^32;
    
    fprintf(FileID,'0x%s\r\n', dec2hex(FileHearder_PWT, 16));             %% Print Data Header
    
    for (Index = 1 : length(GenPWT_Memory_EOP_Only));
        fprintf(FileID,'0x%s\r\n', dec2hex(GenPWT_Memory_EOP_Only(Index), 16));      %% 0x00: EOP Data
    end
    for (Index = 1 : length(GenPWT_Memory_NormalPulse));
        fprintf(FileID,'0x%s\r\n', dec2hex(GenPWT_Memory_NormalPulse(Index), 16));
    end
    fclose(FileID);

    %%---------------------------------Big-End Byte Exchange-------------------------------------------------------------------------
    if (ByteInvEn == 1) 

        FileID = fopen('../output_cmd_script/DMA_PWT_Memory.txt', 'w+');
        fprintf(FileID, '0x%s\r\n', dec2hex(fun_Word64Exchange(FileHearder_PWT), 16));             %% Print Data Header
        fprintf(FileID, '0x%s\r\n', dec2hex(fun_Word64Exchange(GenPWT_Memory_EOP_Only), 16));      %% 0x00: EOP Data

        for (Index = 1 : length(GenPWT_Memory_NormalPulse))
            fprintf(FileID,'0x%s\r\n', dec2hex(fun_Word64Exchange(GenPWT_Memory_NormalPulse(Index)), 16));
        end
        fclose(FileID);
    end 
end
 
%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
%% Generate LT Table
%% LT Table Structure Define
%% 0x0: LineMode for Laser Transimitt, Ultrasound Receive
%% 0x1: LineMode for Ultrasound 
%% 
%% There are two line type number
%% [23:16]: Repeat the Cycles Number (CYCLES[7:0]) 
%% [   15]: LDM = Low Drop Mode,  
%% [   14]: SEQINV bit. Alternate in-phase and out of phase lines when sequencer is enabled.
%% [13: 8]: Program Pulse Width (PW[5:0]), Half of Devider
%% [ 7: 3]: Define number of line iterations of the sequencer (SEQRPT[4:0])
%% [    2]: Continuous Wave Doppler Mode bit (CWD)
%% [ 1: 0]: Set pulser driving current (CC1, CC0], Always 0
%% PWT memory is 0x10
%% Normal Line Type, used for Ultrasound TX-RX Mode 

%% Generate TX Pulse File
DEV_FreqDivderCounter       = floor(SYS_PulseFreq / USR_UltrasoundBaseFreq + 0.5);
DEV_PulseWideCounter        = DEV_FreqDivderCounter / 2;
DEV_PulseDurationTime       = PWT_PaternSize * SYS_PulseTick * DEV_PulseWideCounter * (USR_PatternRptCycleNum + 1);   %%  Include

DMA_LT_TBL_EOP_PatternCycle     = USR_EOP_RptCycleNum;        
DMA_LT_TBL_EOP_PulseWideCNT     = USR_EOP_PulseWidth;
DMA_LT_TBL_EOP_MemoryData       = DMA_LT_TBL_EOP_PatternCycle * 2^16 + DMA_LT_TBL_EOP_PulseWideCNT * 2^8;

DMA_LT_TBL_NormalPatternCycle   = USR_PatternRptCycleNum;
DMA_LT_TBL_NormalPulseWideCNT   = DEV_PulseWideCounter;
DMA_LT_TBL_NormalMemoryData     = DMA_LT_TBL_NormalPatternCycle * 2^16 + DMA_LT_TBL_NormalPulseWideCNT * 2^8;

FileHearder_Length              = USR_FrameNumber * USR_LineNumber;         %% Two LINE Type
FileHearder_LT_DataType         = hex2dec('10');                            %% LT Type is 0x10
FileHearder_LT                  = FileHearder_Length * 2^40 + FileHearder_LT_DataType * 2^32;


%% Only When Non Simulation Mode, Generate Related Data File 
if (GBL_MatlabSimModEN ~= 1) 

    FileID = fopen('../output_cmd_script/DMA_LT_Memory.txt', 'w+');
    fprintf(FileID,'0x%s\r\n', dec2hex(FileHearder_LT, 16));

    %% Each New line with new Different type
    for (LineIndex = 1 : USR_FrameNumber * USR_LineNumber)      
        if (LineIndex == 1)
            fprintf(FileID,'0x%s\r\n', dec2hex(DMA_LT_TBL_EOP_MemoryData  , 16));         %% One TX Tick
        else 
            fprintf(FileID,'0x%s\r\n', dec2hex(DMA_LT_TBL_NormalMemoryData, 16));
        end 
    end
    fclose(FileID);

    %%---------------------------------Big-End Byte Exchange-------------------------------------------------------------------------
    if (ByteInvEn == 1) 
        FileID = fopen('../output_cmd_script/DMA_LT_Memory.txt', 'w+');
        fprintf(FileID,'0x%s\r\n', dec2hex(fun_Word64Exchange(FileHearder_LT), 16));

        %% Each New line with new Different type
        for (LineIndex = 1 : USR_FrameNumber * USR_LineNumber)      
            if (LineIndex == 1)
                fprintf(FileID,'0x%s\r\n', dec2hex(fun_Word64Exchange(DMA_LT_TBL_EOP_MemoryData)  , 16));         %% One TX Tick
            else 
                fprintf(FileID,'0x%s\r\n', dec2hex(fun_Word64Exchange(DMA_LT_TBL_NormalMemoryData), 16));
            end 
        end
        fclose(FileID);
    end
end


%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
%% Generate LN Table
%% Totally 64-Channel, 
%% [23:11]: 13-Bits of Beamforming Delay Value
%% [10: 1]: 10-Bits of PWT Starting Address
%% [    0]: TR_Switch RX Enable Bit
FileHearder_Length              = USR_FrameNumber * USR_LineNumber * SYS_UltrasoundCH;
FileHearder_LN_DataType         = hex2dec('31');    %% LN Type is 0x31
FileHearder_LN                  = FileHearder_Length * 2^40 + FileHearder_LN_DataType * 2^32;          

DMA_LN_TBL_NormalMemoryData     = 0;

%% Generate LN Data
%% Each Channel have different Delay
for (ChannelIndex = 1 : SYS_UltrasoundCH) 

    %% TX Beamforming
    %% ChannelDelay = ChannelIndex - 1 ;        %% One Pulse Delay (5ns)
    ChannelDelay = 0;                           %% Plane Wave Generate
    TR_Switch    = 1;                           %% Always RX ON after TX Transimitt
    
    for (LineIndex = 1 : USR_FrameNumber * USR_LineNumber)
        %% Two PWT Memory
        if (LineIndex == 1)
            Point_PWT_Address = 0;
        else
            Point_PWT_Address =  length(GenPWT_Memory_EOP_Only);
        end 
        DMA_LN_TBL_NormalMemoryData((ChannelIndex - 1) * USR_FrameNumber * USR_LineNumber + LineIndex) = ChannelDelay * 2^11 + Point_PWT_Address * 2^1 + TR_Switch;
    end
end

%% Only When Non Simulation Mode, Generate Related Data File 
if (GBL_MatlabSimModEN ~= 1) 
    FileID = fopen('../output_cmd_script/DMA_LN_Memory.txt', 'w+');
    fprintf(FileID,'0x%s\r\n', dec2hex(FileHearder_LN, 16));
    for (Word24Index = 1 : FileHearder_Length)
        fprintf(FileID,'0x%s\r\n', dec2hex(DMA_LN_TBL_NormalMemoryData(Word24Index)  , 16));
    end
    fclose(FileID);

    %%---------------------------------Big-End Byte Exchange-------------------------------------------------------------------------
    if (ByteInvEn == 1) 
        FileID = fopen('../output_cmd_script/DMA_LN_Memory.txt', 'w+');
        fprintf(FileID,'0x%s\r\n', dec2hex(fun_Word64Exchange(FileHearder_LN), 16));
        for (Word24Index = 1 : FileHearder_Length)
            Temp = fun_Word64Exchange(DMA_LN_TBL_NormalMemoryData(Word24Index));
            fprintf(FileID,'0x%s\r\n', dec2hex(Temp, 16));
        end
        fclose(FileID);
    end 
end 

%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
%% Generate TGC Curve
%% TGC have 8 Segment
%% Need Modify According to TGC Curve
DEV_TGC_DeltaTime = DEV_ACQ_SampleDuration / SYS_TGC_Segment;
for (IndexTGC = 1 : 8)

    DEV_TGC_TimeAxis (IndexTGC)  = DEV_TGC_DeltaTime * (IndexTGC - 1) / 1e-6;    %% Unit: us
    DEV_TGC_DataValue(IndexTGC) = 0;                    

end


%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
%% PCIe Register Configuration List
ADDR_MASTER_TRIG_PERIOD         = '5658';               %% FPGA 1/2/3
    
ADDR_SCAN_PERIOD                = '5008';               %% FPGA 1/2/3 	
ADDR_SCAN_NUMBER                = '5010';               %% FPGA 1/2/3 	
            
ADDR_FRAME_NUMBER               = '5098';               %% FPGA 1/2/3   
ADDR_FRAME_PERIOD               = '50A0';               %% FPGA 1/2/3
ADDR_FRAME_STOP                 = '50A8';               %% FPGA 1/2/3
    
ADDR_ACQ_NUMBER                 = '5038';               %% FPGA 1/2
    
ADDR_PULSER_TRIGGER_DELAY       = '5018';               %% FPGA 1/2 
ADDR_PLUSER_TRIGGER_WIDTH       = '5020';               %% FPGA 1/2
ADDR_ACQ_TRIGGER_DELAY          = '5030';               %% FPGA 1/2
ADDR_TGC_TRIGGER_DELAY      	= '5028';	            %% FPGA 3 
    
ADDR_PROCESS_MDOE               = '5088';               %% FPGA 1/2     , Fixed set to 
ADDR_PARAM_INIT                 = '5090';               %% FPGA 1/2     , Fixed set to 
ADDR_PROCESS_DELAY              = '5130';   

ADDR_LOCAL_MASTER_TRIGGER       = '5050';               %% FPGA 1/2/3   , Start Local Trigger, Need to Config ADDR_MASTER_TRIGGER_NUM_INIT first
ADDR_MASTER_TRIGGER_NUM_INIT    = '5650';               %% FPGA 1/2/3

ADDR_LASER_TX_PULSE_WIDTH       = '5670';               %% FPGA 1/2/3
ADDR_SNL_DELAY                  = '5078';               %% FPGA 1/2
                        
ADDR_TGC_DAC_DATA_1			    = '5100';               %% FPGA 3       , TGC Data 
ADDR_TGC_DAC_DATA_2			    = '5108';               %% FPGA 3 
ADDR_TGC_DAC_DATA_3			    = '5110';               %% FPGA 3 
ADDR_TGC_DAC_DATA_4			    = '5118';               %% FPGA 3 
ADDR_TGC_DAC_DATA_5			    = '5120';               %% FPGA 3 
ADDR_TGC_DAC_DATA_6			    = '5128';               %% FPGA 3 
ADDR_TGC_DAC_DATA_7			    = '5130';               %% FPGA 3 
ADDR_TGC_DAC_DATA_8			    = '5138';	            %% FPGA 3 

                  
%% Configration Register List
REG_MASTER_TRIGGER_NUM_INIT     = USR_ExtTrigNumber;
REG_MASTER_TRIG_PERIOD          = USR_ExtTriggerPeriod / SYS_PCIeTick;  

REG_FRAME_NUMBER                = USR_FrameNumber;
REG_FRAME_PERIOD                = DEV_FramePeriod / SYS_PulseTick;

REG_SCAN_NUMBER                 = USR_LineNumber;
REG_SCAN_PERIOD                 = DEV_LinePeriod / SYS_PulseTick;

REG_ACQ_NUMBER                  = floor(DEV_ACQ_SampleNumber);

REG_PULSER_TRIGGER_DELAY        = 2;
REG_PLUSER_TRIGGER_WIDTH        = DEV_PulseDurationTime / SYS_PulseTick + SYS_PulserConstantLatency / SYS_PulseTick;

REG_ACQ_TRIGGER_DELAY           = REG_PLUSER_TRIGGER_WIDTH + DEV_Pulse2AFE_GlithCNT;               %% FPGA 1/2
REG_TGC_TRIGGER_DELAY       	= REG_PLUSER_TRIGGER_WIDTH + DEV_Pulse2AFE_GlithCNT;	           %% FPGA 3 

REG_PROCESS_DELAY               = SYS_DataProcessDelay;          
REG_PROCESS_MDOE                = 2;                %% FPGA 1/2     , Fixed set to 2, Constant 
REG_PARAM_INIT                  = 1;                %% FPGA 1/2     , Fixed set to 1

REG_TGC_DAC_DATA_1              = floor(2^32 * DEV_TGC_TimeAxis(1) + DEV_TGC_DataValue(1));
REG_TGC_DAC_DATA_2              = floor(2^32 * DEV_TGC_TimeAxis(2) + DEV_TGC_DataValue(2));
REG_TGC_DAC_DATA_3              = floor(2^32 * DEV_TGC_TimeAxis(3) + DEV_TGC_DataValue(3));
REG_TGC_DAC_DATA_4              = floor(2^32 * DEV_TGC_TimeAxis(4) + DEV_TGC_DataValue(4));
REG_TGC_DAC_DATA_5              = floor(2^32 * DEV_TGC_TimeAxis(5) + DEV_TGC_DataValue(5));
REG_TGC_DAC_DATA_6              = floor(2^32 * DEV_TGC_TimeAxis(6) + DEV_TGC_DataValue(6));
REG_TGC_DAC_DATA_7              = floor(2^32 * DEV_TGC_TimeAxis(7) + DEV_TGC_DataValue(7));
REG_TGC_DAC_DATA_8              = floor(2^32 * DEV_TGC_TimeAxis(8) + DEV_TGC_DataValue(8));

REG_LOCAL_MASTER_TRIGGER        = 1;                %% Fixed 1, Lanuch Internal Master Trigger (Number of Trigger Num Equtal to REG_MASTER_TRIGGER_NUM_INIT)

REG_LASER_TX_PULSE_WIDTH        = USR_LaserTX_PulseWidth + SYS_PulserConstantLatency / SYS_PulseTick;
REG_SNL_DELAY                   = SYS_SNL_DelayCnt      ;              
            
%% Auto Generate Command File
%% Pulser DMA
if (GBL_MatlabSimModEN ~= 1)

    FileID = fopen('../output_cmd_script/REG_CMD_Line_Config.vh', 'w+');
    fprintf(FileID, '//-----------------------------------------------------------------------------------------------------------------\r');
    fprintf(FileID, '//-----------------------------------------------------------------------------------------------------------------\r');
    fprintf(FileID, '// Pulser DMA Config Parameter\r');
    fprintf(FileID, 'DMAIN ./DMA_LT_Memory.txt\r');         %% Auto Generated
    fprintf(FileID, 'DMAIN ./DMA_LN_Memory.txt\r');         %% Auto Generated
    fprintf(FileID, 'DMAIN ./DMA_PWT_Memory.txt\r');        %% Auto Generated

    %% Pulser SPI REG 
    fprintf(FileID, '\r\n// Pulser Register Config Parameter\r');
    fprintf(FileID, 'Down  ./Down_PULSER_SPI_REG.txt\r');   %% Constant File

    %% SPI DIG EN (Hardwire)
    fprintf(FileID, '\r\n// 0x4i78, First Write SPI_DIG_EN = 0x1 to Enable SPI ADC Reg Config\r');      
    fprintf(FileID, 'Write 1 0x4078 0x1\r');                                      
    fprintf(FileID, 'Write 1 0x4178 0x1\r');
    fprintf(FileID, 'Write 1 0x4278 0x1\r');
    fprintf(FileID, 'Write 1 0x4378 0x1\r');
    fprintf(FileID, '\r\n'                  );
    fprintf(FileID, 'Write 2 0x4078 0x1\r');                             
    fprintf(FileID, 'Write 2 0x4178 0x1\r');
    fprintf(FileID, 'Write 2 0x4278 0x1\r');
    fprintf(FileID, 'Write 2 0x4378 0x1\r');

    %% AFE Config with Traing
    fprintf(FileID, '\r\n//-----------------------------------------------------------------------------------------------------------------\r');
    fprintf(FileID, '// Set AFE Chip via Register\r');
    fprintf(FileID, 'Down ./Down_AFE_CONFIG_I.txt\r');      %% Constant File
    fprintf(FileID, 'Down ./Down_AFE_CONFIG_II.txt\r');     %% Constant File 
    fprintf(FileID, 'Down ./Down_AFE_CONFIG_III.txt\r');    %% Constant File 
    fprintf(FileID, 'Down ./Down_AFE_CONFIG_IV.txt\r');     %% Constant File

    %% AFE ACQ Algn
    fprintf(FileID, '\r\n// Trig Sync Data Align AFE  at Beginning\r');
    fprintf(FileID, 'Write 1 0x5040 0x1\r');                 %% Constant File
    fprintf(FileID, 'Write 2 0x5040 0x1\r');                 %% Constant File

          
    %% Waiting for Enough time for AFE Trainning, %% Revover AFE to Normal Acquisition
    fprintf(FileID, '\r\n// Recover to AFE Normal Mode (Recover of ADC to Normal Acquisiton Mode from Trainning Mode)\r');
    fprintf(FileID, '// 0x4i80 Register Base Address, 8Bit Address + 16 Bit Data\r'); 
    fprintf(FileID, '// Write 1 0x4i80 0x020000, AFE1, 0x020000 (0x_AFE_8_BIT_ADDR[23:16]_16_BIT_DATA[15:0]), Normal Mode\r'); 
    fprintf(FileID, 'Write 1 0x4080 0x020000\r');                                      
    fprintf(FileID, 'Write 1 0x4180 0x020000\r');
    fprintf(FileID, 'Write 1 0x4280 0x020000\r');
    fprintf(FileID, 'Write 1 0x4380 0x020000\r');
    fprintf(FileID, '\r'                  );
    fprintf(FileID, 'Write 2 0x4080 0x020000\r');                             
    fprintf(FileID, 'Write 2 0x4180 0x020000\r');
    fprintf(FileID, 'Write 2 0x4280 0x020000\r');
    fprintf(FileID, 'Write 2 0x4380 0x020000\r');

    %% Generate ACQ Parameter Configration
    fprintf(FileID, '\r\n//-----------------------------------------------------------------------------------------------------------------\r');
    fprintf(FileID, '// Generate ACQ Parameter Configration\r');
    fprintf(FileID, '// Scan Parameter Configration, FPGA 1/2/3\r');
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_MASTER_TRIG_PERIOD\r', ADDR_MASTER_TRIG_PERIOD  , dec2hex(floor(REG_MASTER_TRIG_PERIOD  ),  8)); 
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_FRAME_NUMBER      \r', ADDR_FRAME_NUMBER        , dec2hex(floor(REG_FRAME_NUMBER        ),  8)); 
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_FRAME_PERIOD      \r', ADDR_FRAME_PERIOD        , dec2hex(floor(REG_FRAME_PERIOD        ),  8)); 
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_SCAN_NUMBER       \r', ADDR_SCAN_NUMBER         , dec2hex(floor(REG_SCAN_NUMBER         ),  8)); 
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_SCAN_PERIOD       \r', ADDR_SCAN_PERIOD         , dec2hex(floor(REG_SCAN_PERIOD         ),  8)); 
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_LASER_TX_PULSE_WIDTH\r',ADDR_LASER_TX_PULSE_WIDTH, dec2hex(floor(REG_LASER_TX_PULSE_WIDTH),8)); 
    fprintf(FileID, '\r'                  );
                                                                                                                 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_MASTER_TRIG_PERIOD \r', ADDR_MASTER_TRIG_PERIOD  , dec2hex(floor(REG_MASTER_TRIG_PERIOD  ),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_FRAME_NUMBER       \r', ADDR_FRAME_NUMBER        , dec2hex(floor(REG_FRAME_NUMBER        ),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_FRAME_PERIOD       \r', ADDR_FRAME_PERIOD        , dec2hex(floor(REG_FRAME_PERIOD        ),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_SCAN_NUMBER        \r', ADDR_SCAN_NUMBER         , dec2hex(floor(REG_SCAN_NUMBER         ),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_SCAN_PERIOD        \r', ADDR_SCAN_PERIOD         , dec2hex(floor(REG_SCAN_PERIOD         ),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_LASER_TX_PULSE_WIDTH\r', ADDR_LASER_TX_PULSE_WIDTH, dec2hex(floor(REG_LASER_TX_PULSE_WIDTH),  8)); 
    fprintf(FileID, '\r'                  );
                                                                                                               
    fprintf(FileID, 'Write 3 0x%s 0x%s    // ADDR_MASTER_TRIG_PERIOD \r', ADDR_MASTER_TRIG_PERIOD  , dec2hex(floor(REG_MASTER_TRIG_PERIOD  ),  8)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s    // ADDR_FRAME_NUMBER       \r', ADDR_FRAME_NUMBER        , dec2hex(floor(REG_FRAME_NUMBER        ),  8)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s    // ADDR_FRAME_PERIOD       \r', ADDR_FRAME_PERIOD        , dec2hex(floor(REG_FRAME_PERIOD        ),  8)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s    // ADDR_SCAN_NUMBER        \r', ADDR_SCAN_NUMBER         , dec2hex(floor(REG_SCAN_NUMBER         ),  8)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s    // ADDR_SCAN_PERIOD        \r', ADDR_SCAN_PERIOD         , dec2hex(floor(REG_SCAN_PERIOD         ),  8)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s    // ADDR_LASER_TX_PULSE_WIDTH\r', ADDR_LASER_TX_PULSE_WIDTH, dec2hex(floor(REG_LASER_TX_PULSE_WIDTH),  8)); 
    fprintf(FileID, '\r'                  ); 
                                                                                                                
    %% AFE & Pulser Config, FPGA 1/2 Only                                                                       
    fprintf(FileID, '\r\n// AFE & Pulser Config, FPGA 1/2 Only\r');                                               
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_PULSER_TRIGGER_DELAY\r', ADDR_PULSER_TRIGGER_DELAY, dec2hex(floor(REG_PULSER_TRIGGER_DELAY),  8)); 
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_PLUSER_TRIGGER_WIDTH\r', ADDR_PLUSER_TRIGGER_WIDTH, dec2hex(floor(REG_PLUSER_TRIGGER_WIDTH),  8)); 
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_ACQ_TRIGGER_DELAY   \r', ADDR_ACQ_TRIGGER_DELAY   , dec2hex(floor(REG_ACQ_TRIGGER_DELAY   ),  8)); 
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_PROCESS_DELAY       \r', ADDR_PROCESS_DELAY       , dec2hex(floor(REG_PROCESS_DELAY       ),  8)); 
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_PROCESS_MDOE        \r', ADDR_PROCESS_MDOE        , dec2hex(floor(REG_PROCESS_MDOE        ),  8)); 
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_PARAM_INIT          \r', ADDR_PARAM_INIT          , dec2hex(floor(REG_PARAM_INIT          ),  8)); 
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_ACQ_NUMBER          \r', ADDR_ACQ_NUMBER          , dec2hex(floor(REG_ACQ_NUMBER          ),  8));
    fprintf(FileID, 'Write 1 0x%s 0x%s    // ADDR_SNL_DELAY           \r', ADDR_SNL_DELAY           , dec2hex(floor(REG_SNL_DELAY           ),  8));   
    fprintf(FileID, '\r'                  );                                                                                                          

    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_PULSER_TRIGGER_DELAY\r', ADDR_PULSER_TRIGGER_DELAY, dec2hex(floor(REG_PULSER_TRIGGER_DELAY),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_PLUSER_TRIGGER_WIDTH\r', ADDR_PLUSER_TRIGGER_WIDTH, dec2hex(floor(REG_PLUSER_TRIGGER_WIDTH),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_ACQ_TRIGGER_DELAY   \r', ADDR_ACQ_TRIGGER_DELAY   , dec2hex(floor(REG_ACQ_TRIGGER_DELAY   ),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_PROCESS_DELAY       \r', ADDR_PROCESS_DELAY       , dec2hex(floor(REG_PROCESS_DELAY       ),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_PROCESS_MDOE        \r', ADDR_PROCESS_MDOE        , dec2hex(floor(REG_PROCESS_MDOE        ),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_PARAM_INIT          \r', ADDR_PARAM_INIT          , dec2hex(floor(REG_PARAM_INIT          ),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_ACQ_NUMBER          \r', ADDR_ACQ_NUMBER          , dec2hex(floor(REG_ACQ_NUMBER          ),  8)); 
    fprintf(FileID, 'Write 2 0x%s 0x%s    // ADDR_SNL_DELAY           \r', ADDR_SNL_DELAY           , dec2hex(floor(REG_SNL_DELAY           ),  8));   
    fprintf(FileID, '\r'                  ); 

    %% TGC Config, FPGA 3 Only
    fprintf(FileID, '\r\n// TGC Config, FPGA 3 Only\r');
    
    fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_TGC_TRIGGER_DELAY   , dec2hex(floor(REG_ACQ_TRIGGER_DELAY   ),  8)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_TGC_DAC_DATA_1      , dec2hex(floor(REG_TGC_DAC_DATA_1      ), 16)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_TGC_DAC_DATA_2      , dec2hex(floor(REG_TGC_DAC_DATA_2      ), 16)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_TGC_DAC_DATA_3      , dec2hex(floor(REG_TGC_DAC_DATA_3      ), 16)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_TGC_DAC_DATA_4      , dec2hex(floor(REG_TGC_DAC_DATA_4      ), 16)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_TGC_DAC_DATA_5      , dec2hex(floor(REG_TGC_DAC_DATA_5      ), 16)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_TGC_DAC_DATA_6      , dec2hex(floor(REG_TGC_DAC_DATA_6      ), 16)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_TGC_DAC_DATA_7      , dec2hex(floor(REG_TGC_DAC_DATA_7      ), 16)); 
    fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_TGC_DAC_DATA_8      , dec2hex(floor(REG_TGC_DAC_DATA_8      ), 16)); 

    fprintf(FileID, '\r\nSetFrameSize  0x400000\r');        %% Set Memory Size
    fprintf(FileID, 'SetFrameNum  10000\r');                %% Set Acq Number File-Size



    %% Launch Command for AFE Acquisiton 
    fprintf(FileID, '\r\n// Launch Command for AFE Acquisiton\r');
    fprintf(FileID, 'PrepareforACQ\r');


    %% Note:
    %% Set USR_EXTTriggerSlect 1: Trigger From Internal FPGA1
    %% Set USR_EXTTriggerSlect 2: Trigger From Internal FPGA2
    %% Set USR_EXTTriggerSlect 3: Trigger From Exteranl FPGA3 -> FFPGA1/2, ACQ 128 Channel
    %% Set USR_EXTTriggerSlect 4: Trigger From Internal FPGA3 -> FFPGA1/2, ACQ 128 Channel 

    %% Pulser SPI REG 
    fprintf(FileID, '\r\n// Pulser SNL Initial Begin Config Parameter\r');
    fprintf(FileID, 'Down  ./Down_PULSER_SPI_SNL.txt\r');   %% Constant File

                                 
    switch (USR_EXTTriggerSlect)
        case (1)
            fprintf(FileID, '\r\n// Set USR_EXTTriggerSlect 1: Trigger From Internal FPGA1\r');
            fprintf(FileID, 'Write 1 0x%s 0x%s\r', ADDR_MASTER_TRIGGER_NUM_INIT, dec2hex(floor(REG_MASTER_TRIGGER_NUM_INIT), 4)); 
            fprintf(FileID, 'Write 1 0x%s 0x%s\r', ADDR_LOCAL_MASTER_TRIGGER   , dec2hex(floor(REG_LOCAL_MASTER_TRIGGER   ), 4)); 
        case (2)
            fprintf(FileID, '\r\n// Set USR_EXTTriggerSlect 2: Trigger From Internal FPGA1\r');
            fprintf(FileID, 'Write 2 0x%s 0x%s\r', ADDR_MASTER_TRIGGER_NUM_INIT, dec2hex(floor(REG_MASTER_TRIGGER_NUM_INIT), 4)); 
            fprintf(FileID, 'Write 2 0x%s 0x%s\r', ADDR_LOCAL_MASTER_TRIGGER   , dec2hex(floor(REG_LOCAL_MASTER_TRIGGER   ), 4)); 
        case (3)
            fprintf(FileID, '\r\n// Set USR_EXTTriggerSlect 3: Trigger From Exteranl FPGA3 -> FFPGA1/2, ACQ 128 Channel\r');
            fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_MASTER_TRIGGER_NUM_INIT, dec2hex(floor(REG_MASTER_TRIGGER_NUM_INIT), 4)); 
        case (4)
            fprintf(FileID, '\r\n// Set USR_EXTTriggerSlect 4: Trigger From Internal FPGA3 -> FFPGA1/2, ACQ 128 Channel\r');
            fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_MASTER_TRIGGER_NUM_INIT, dec2hex(floor(REG_MASTER_TRIGGER_NUM_INIT), 4)); 
            fprintf(FileID, 'Write 3 0x%s 0x%s\r', ADDR_LOCAL_MASTER_TRIGGER   , dec2hex(floor(REG_LOCAL_MASTER_TRIGGER   ), 4)); 
        
        otherwise
            fprintf(FileID, '\r\n// Set USR_EXTTriggerSlect 1: Trigger From Internal FPGA1\r');
            fprintf(FileID, 'Write 1 0x%s 0x%s\r', ADDR_MASTER_TRIGGER_NUM_INIT, dec2hex(floor(REG_MASTER_TRIGGER_NUM_INIT), 4)); 
            fprintf(FileID, 'Write 1 0x%s 0x%s\r', ADDR_LOCAL_MASTER_TRIGGER   , dec2hex(floor(REG_LOCAL_MASTER_TRIGGER   ), 4)); 
    end

    fclose(FileID);
end 

%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
%% 
%% Plot Scan Waveform, GBL_MatlabSimModEN Configration
if (GBL_MatlabSimModEN == 1)

    SimTick                         = 1e-9;
    t                               = 0: SimTick : (USR_ExtTrigNumber * USR_ExtTriggerPeriod - SimTick);     
    FullSize                        = length(t);

    %%-------------------------------------------------------------------------------------------------------------------------------         
    %% Generate Exteanal Trigger Waveform          
    EXT_Trig_Width                  = 10;                                          %% Nommalize Width to 0.1us Ticks 
        
    EXT_PeriodCNT                   = floor((SYS_PCIeTick * REG_MASTER_TRIG_PERIOD / SimTick));
    EXT_Delay                       = 2000;
    EXT_Signal                      = ones(1, EXT_Trig_Width);
    EXT_Repeat_N                    = floor(REG_MASTER_TRIGGER_NUM_INIT);
            
    [DataSize WAV_ExtTrig]          = fun_GenPeriodWavform(EXT_PeriodCNT, EXT_Delay, EXT_Signal, EXT_Repeat_N);

    %%-------------------------------------------------------------------------------------------------------------------------------         
    %% Generate Frame Waveform          
    FRAME_Trig_Width                = 100;                                              %% Nommalize Width to 0.1us Ticks 
                                    
    FRAME_PeriodCNT                 = floor((SYS_PulseTick * REG_FRAME_PERIOD / SimTick));
    FRAME_Delay                     = EXT_Trig_Width + EXT_Delay;
    FRAME_Signal                    = ones(1, FRAME_Trig_Width);
    FRAME_Repeat_N                  = floor(REG_FRAME_NUMBER);
    
    [DataSize WAV_FrameTrig_Cell]   = fun_GenPeriodWavform(FRAME_PeriodCNT, FRAME_Delay, FRAME_Signal     ,  FRAME_Repeat_N);
    [DataSize WAV_FrameTrig     ]   = fun_GenPeriodWavform(EXT_PeriodCNT  , 0          , WAV_FrameTrig_Cell, EXT_Repeat_N  );


    %%-------------------------------------------------------------------------------------------------------------------------------         
    %% Generate Scan Trigger Waveform          
    SCAN_Trig_Width                 = 100;                                              %% Nommalize Width to 0.1us Ticks 
                                    
    SCAN_PeriodCNT                  = floor((SYS_PulseTick * REG_SCAN_PERIOD / SimTick));
    SCAN_Delay                      = EXT_Trig_Width + EXT_Delay;
    SCAN_Signal                     = ones(1, SCAN_Trig_Width);
    SCAN_Repeat_N                   = floor(REG_SCAN_NUMBER);
    
    [DataSize WAV_ScanTrig_Cell_0]  = fun_GenPeriodWavform(SCAN_PeriodCNT,  SCAN_Delay , SCAN_Signal        ,  SCAN_Repeat_N );
    [DataSize WAV_ScanTrig_Cell_1]  = fun_GenPeriodWavform(FRAME_PeriodCNT, FRAME_Delay, WAV_ScanTrig_Cell_0,  FRAME_Repeat_N);
    [DataSize WAV_ScanTrig       ]  = fun_GenPeriodWavform(EXT_PeriodCNT  , 0          , WAV_ScanTrig_Cell_1,  EXT_Repeat_N  );


    %%-------------------------------------------------------------------------------------------------------------------------------         
    %% Generate Pulser Trigger Waveform   
    PULSE_NML_Trig_Width            = floor((SYS_PulseTick * REG_PLUSER_TRIGGER_WIDTH / SimTick));

    PULSE_PeriodCNT                 = floor((SYS_PulseTick * REG_SCAN_PERIOD / SimTick));
    PULSE_Delay                     = EXT_Trig_Width + EXT_Delay + REG_PULSER_TRIGGER_DELAY;
    PULSE_Signal                    = ones(1, PULSE_NML_Trig_Width);
    PULSE_Repeat_N                  = floor(REG_SCAN_NUMBER); 
    
    [DataSize WAV_PulsTrig_Cell_0]  = fun_GenPeriodWavform(PULSE_PeriodCNT, PULSE_Delay, PULSE_Signal       ,  PULSE_Repeat_N);
    [DataSize WAV_PulsTrig_Cell_1]  = fun_GenPeriodWavform(FRAME_PeriodCNT, FRAME_Delay, WAV_PulsTrig_Cell_0,  FRAME_Repeat_N);
    [DataSize WAV_PulsTrig       ]  = fun_GenPeriodWavform(EXT_PeriodCNT  , 0          , WAV_PulsTrig_Cell_1,  EXT_Repeat_N  );


    %% Generate Pulse NOP Trigger 
    LASER_TX_Trig_Width             = floor((SYS_PulseTick * REG_LASER_TX_PULSE_WIDTH / SimTick));
    LASER_TX_Delay                  = EXT_Trig_Width + EXT_Delay + REG_PULSER_TRIGGER_DELAY;
    LASER_TX_Signal                 = ones(1, LASER_TX_Trig_Width);    
    LASER_TX_Repeat_N               = 1;
    
    [DataSize WAV_LaserTX_Cell_0]   = fun_GenPeriodWavform(PULSE_PeriodCNT, LASER_TX_Delay, LASER_TX_Signal ,  LASER_TX_Repeat_N);
    
    %% Replace TX Trigger firt Pulse Using WAV_LaserTX_Cell_0 After Each of Trigger Cycle 
    for (i = 1 : EXT_Repeat_N)        
        ReplaceLen = length(WAV_LaserTX_Cell_0);     
        for (j = 1 : ReplaceLen)
            WAV_PulsTrig((i - 1) *  EXT_PeriodCNT  + j) =  WAV_LaserTX_Cell_0(j);        
        end 
    end 
 
    %%-------------------------------------------------------------------------------------------------------------------------------         
    %% Generate Pulser Trigger Signal Waveform  
    [DataLen PulseSig_Basic_Repeat] = fun_GenPulseSigBasicRepeat(USR_PulsePattern_Code, DEV_PulseWideCounter * 5, (USR_PatternRptCycleNum + 1), 1);
    
    TX_SIG_PeriodCNT                = floor((SYS_PulseTick * REG_SCAN_PERIOD / SimTick));
    %% Signal With External "SYS_PulserConstantLatency = (26+64) * Pulser Clock Delay = 450"
    TX_SIG_Delay                    = EXT_Trig_Width + EXT_Delay + REG_PULSER_TRIGGER_DELAY + floor(SYS_PulserConstantLatency / SimTick);
    TX_SIG_Signal                   = PulseSig_Basic_Repeat;
    TX_SIG_Repeat_N                 = floor(REG_SCAN_NUMBER); 
    
    [DataSize WAV_TX_SIG_Cell_0]    = fun_GenPeriodWavform(TX_SIG_PeriodCNT, TX_SIG_Delay, TX_SIG_Signal       ,  TX_SIG_Repeat_N);
    [DataSize WAV_TX_SIG_Cell_1]    = fun_GenPeriodWavform(FRAME_PeriodCNT , FRAME_Delay , WAV_TX_SIG_Cell_0   ,  FRAME_Repeat_N );
    [DataSize WAV_TX_SIG       ]    = fun_GenPeriodWavform(EXT_PeriodCNT   , 0           , WAV_TX_SIG_Cell_1    , EXT_Repeat_N   );
    
    
    %% Generate Pulse NOP Sign
    [DataLen EOP_Sig_Basic_Repeat] = fun_GenPulseSigBasicRepeat(USR_PulsePattern_EOP, DEV_PulseWideCounter * 1, (USR_PatternRptCycleNum + 1), 1);
    
    EOP_SIG_PeriodCNT                = floor((SYS_PulseTick * REG_SCAN_PERIOD / SimTick));
    %% Signal With External "SYS_PulserConstantLatency = (26+64) * Pulser Clock Delay = 450", Delay Fixed Latency, then Output Wave
    EOP_SIG_Delay                    = EXT_Trig_Width + EXT_Delay + REG_PULSER_TRIGGER_DELAY + floor(SYS_PulserConstantLatency / SimTick);
    EOP_SIG_Signal                   = EOP_Sig_Basic_Repeat;
    EOP_SIG_Repeat_N                 = 1; 
    
    [DataSize WAV_EOP_SIG_Cell_0]    = fun_GenPeriodWavform(EOP_SIG_PeriodCNT, EOP_SIG_Delay, EOP_SIG_Signal       ,  EOP_SIG_Repeat_N);   

    %% Replace TX Signal of firt Pulse Using WAV_EOP_SIG_Cell_0 After Each of Trigger Cycle 
    for (i = 1 : EXT_Repeat_N)        
        ReplaceLen = length(WAV_EOP_SIG_Cell_0);     
        for (j = 1 : ReplaceLen)
            WAV_TX_SIG((i - 1) *  EXT_PeriodCNT  + j) =  WAV_EOP_SIG_Cell_0(j);        
        end 
    end 


    %%-------------------------------------------------------------------------------------------------------------------------------         
    %% Generate ACQ Trigger Waveform   
    ACQ_Trig_Width                  = floor(DEV_ACQ_SampleDuration / SimTick);

    ACQ_Trig_PeriodCNT              = floor((SYS_PulseTick * REG_SCAN_PERIOD / SimTick));
    ACQ_Trig_Delay                  = floor(EXT_Trig_Width + EXT_Delay + REG_ACQ_TRIGGER_DELAY * 5);
    ACQ_Trig_Signal                 = ones(1, ACQ_Trig_Width);
    ACQ_Trig_Repeat_N               = floor(REG_SCAN_NUMBER); 
    
    [DataSize WAV_ACQ_Trig_Cell_0]  = fun_GenPeriodWavform(ACQ_Trig_PeriodCNT, ACQ_Trig_Delay, ACQ_Trig_Signal       ,  ACQ_Trig_Repeat_N);
    [DataSize WAV_ACQ_Trig_Cell_1]  = fun_GenPeriodWavform(FRAME_PeriodCNT   , FRAME_Delay   , WAV_ACQ_Trig_Cell_0   ,  FRAME_Repeat_N   );
    [DataSize WAV_ACQ_Trig       ]  = fun_GenPeriodWavform(EXT_PeriodCNT     , 0             , WAV_ACQ_Trig_Cell_1   ,  EXT_Repeat_N     );


    %% Generate Pulse NOP Trigger 
    LASER_ACQ_Trig_Width             = floor((DEV_ACQ_SampleDuration / SimTick));
    
    LASER_ACQ_PeriodCNT              = floor((SYS_PulseTick * REG_SCAN_PERIOD / SimTick));
    LASER_ACQ_Delay                  = floor(EXT_Trig_Width + EXT_Delay + REG_LASER_TX_PULSE_WIDTH * 5);
    LASER_ACQ_Signal                 = ones(1, LASER_ACQ_Trig_Width);    
    LASER_ACQ_Repeat_N               = 1;
    
    [DataSize LASER_ACQ_Cell_0]   = fun_GenPeriodWavform(LASER_ACQ_PeriodCNT, LASER_ACQ_Delay, LASER_ACQ_Signal ,  LASER_ACQ_Repeat_N);
    
    %% Replace TX Trigger firt Pulse Using WAV_LaserTX_Cell_0 After Each of Trigger Cycle 
    for (i = 1 : EXT_Repeat_N)        
        ReplaceLen = length(LASER_ACQ_Cell_0);     
        for (j = 1 : ReplaceLen)
            WAV_ACQ_Trig((i - 1) *  EXT_PeriodCNT  + j) =  LASER_ACQ_Cell_0(j);        
        end 
    end 


    
    %%-------------------------------------------------------------------------------------------------------------------------------         
    %% Generate Ultrasound Signal & Clock Signal
    n = 1 : length(WAV_ACQ_Trig);
    WAV_ACQ_Sine_Cell = sin(2 * pi * USR_UltrasoundBaseFreq * n * SYS_AFEClkTick);
    WAV_ACQ_Sine      = WAV_ACQ_Sine_Cell .* WAV_ACQ_Trig;
    
    for (n = 1 : length(WAV_ACQ_Trig))
        if (mod(n, 2) == 0)
            WAV_ClkTick(n) = 0;
        else 
            WAV_ClkTick(n) = 1;
        end
    end

    %%-------------------------------------------------------------------------------------------------------------------------------         
    %% Process Signal for Displa Organzized
    Display_WAV_ClkTick     = WAV_ClkTick   *   0.85    +   10;
    Display_WAV_ExtTrig     = WAV_ExtTrig   *   0.85    +   9 ;
    Display_WAV_FrameTrig   = WAV_FrameTrig *   0.85    +   8 ;    
    Display_WAV_ScanTrig    = WAV_ScanTrig  *   0.85    +   7 ;     
    Display_WAV_PulsTrig    = WAV_PulsTrig  *   0.85    +   6 ;  
    Display_WAV_TX_SIG      = WAV_TX_SIG    *   0.85    +   5 ;  
    Display_WAV_ACQ_Trig    = WAV_ACQ_Trig  *   0.85    +   3 ;  
    Display_WAV_ACQ_Sine    = WAV_ACQ_Sine  *   0.85    +   2 ;  


    %%-------------------------------------------------------------------------------------------------------------------------------         
    %% Generate Pulser Trigger Waveform    
    figure;
    n = 1 : length(WAV_ExtTrig);
    
    plot(  n, Display_WAV_ClkTick   ,   'b-',   ...
           n, Display_WAV_ExtTrig   ,   'b.-',  ...
           n, Display_WAV_FrameTrig ,   'b.-',  ...
           n, Display_WAV_ScanTrig  ,   'b.-',  ...
           n, Display_WAV_PulsTrig  ,   'b.-',  ...
           n, Display_WAV_TX_SIG    ,   'b.-',  ...
           n, Display_WAV_ACQ_Trig  ,   'b.-',  ...
           n, Display_WAV_ACQ_Sine  ,   'b-');
     grid on;

    legend(  'WAV\_ClkTick   ',  ...
             'WAV\_ExtTrig   ',  ...
             'WAV\_FrameTrig ',  ...
             'WAV\_ScanTrig  ',  ...
             'WAV\_PulsTrig  ',  ...
             'WAV\_TX\_SIG    ',  ...
             'WAV\_ACQ\_Trig  ',  ...
             'WAV\_ACQ\_Sine  ');
           


end
